package torres.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;

import torres.entity.Ability;
import torres.entity.Avatar;
import torres.entity.Occupation;
import torres.item.EquippableItem;
import torres.item.EquippedInventory;
import torres.item.TakeableItem;
import torres.map.World;

/**
 * @author Torres
 * Represents the status view port. In other words how the
 * inventory and equipped inventory looks like 
 */

@SuppressWarnings("serial")
public class StatusViewPort extends JPanel {
	
	private JTextArea textArea;
	private JPanel inventory;
	private JPanel equipment;
	private JPanel abilityPane;
	private JLabel head = new JLabel();
	private JLabel left = new JLabel();
	private JLabel right = new JLabel();
	private JLabel upper = new JLabel();
	private JLabel lower = new JLabel();
	Avatar avatar;
	StringBuilder text;
	private int AREA_WIDTH = 279;
	private int TEXT_HEIGHT = 215;
	private int INVENTORY_HEIGHT = 280;
	private int TOTAL_HEIGHT = TEXT_HEIGHT+INVENTORY_HEIGHT;
	
	/**
	 * Creates a new StatusViewPort with the default settings.
	 */
	public StatusViewPort() {
		this.setBackground(Color.BLACK);
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setFont(initAndGetFont() );
		textArea.setPreferredSize(new Dimension(AREA_WIDTH, TEXT_HEIGHT));
		textArea.setBounds(0, 0, AREA_WIDTH, TEXT_HEIGHT);
		JTabbedPane tabbedPane = new JTabbedPane();
		UIManager.put("TabbedPane.selected",
	               new ColorUIResource(Color.GRAY));
		UIManager.put("TabbedPane.highlight",
	               new ColorUIResource(Color.GRAY));
		SwingUtilities.updateComponentTreeUI(tabbedPane);
		tabbedPane.setPreferredSize(new Dimension(AREA_WIDTH, INVENTORY_HEIGHT));
		tabbedPane.setBounds(0, TEXT_HEIGHT, AREA_WIDTH, INVENTORY_HEIGHT);
		inventory = new JPanel();
		inventory.setBackground(Color.BLACK);
		inventory.setLayout(new GridLayout(5, 3));
		for(int i = 0; i < 15; ++i) {
			inventory.add(new JLabel());
		}
		equipment = new JPanel();
		equipment.setLayout(new GridLayout(3, 3));
		equipment.setPreferredSize(new Dimension(AREA_WIDTH, INVENTORY_HEIGHT));
		equipment.setBounds(0, 0, AREA_WIDTH, INVENTORY_HEIGHT);
		head.setSize(100,100);
		//head.setBounds(100,50,50,50);
		head.setHorizontalAlignment(SwingConstants.CENTER);
		left.setSize(100,100);
		//left.setBounds(25,150,50,50);
		left.setHorizontalAlignment(SwingConstants.CENTER);
		right.setSize(100,100);
		//right.setBounds(175,150,50,50);
		right.setHorizontalAlignment(SwingConstants.CENTER);
		upper.setSize(100,100);
		//upper.setBounds(100,125,50,50);
		upper.setHorizontalAlignment(SwingConstants.CENTER);
		lower.setSize(100,100);
		//lower.setBounds(100,200,50,50);
		lower.setHorizontalAlignment(SwingConstants.CENTER);
		head.setForeground(new Color(0, 255, 0));
		left.setForeground(new Color(0, 255, 0));
		right.setForeground(new Color(0, 255, 0));
		upper.setForeground(new Color(0, 255, 0));
		lower.setForeground(new Color(0, 255, 0));
		equipment.add(new JLabel());
		equipment.add(head);
		equipment.add(new JLabel());
		equipment.add(left);
		equipment.add(upper);
		equipment.add(right);
		equipment.add(new JLabel());
		equipment.add(lower);
		equipment.add(new JLabel());
		
		equipment.setBackground(Color.BLACK);
		tabbedPane.setBackground(Color.BLACK);
		tabbedPane.setForeground(Color.WHITE);
		equipment.setForeground(new Color(0, 255, 0));
		
		Occupation occupation = World.getInstance().getCurrentMap().getAvatar().getOccupation();
		List<Ability> abilities = occupation.getAbilityList();
		abilityPane = new JPanel();
		abilityPane.setBackground(Color.BLACK);
		abilityPane.setForeground(new Color(0, 255, 0));
		abilityPane.setLayout(new GridLayout(3, 3));
		for(int i = 0; i < abilities.size(); ++i) {
			JLabel ability = new JLabel(abilities.get(i).getName());
			ability.setToolTipText("Level unlocked: "+abilities.get(i).getLevelUnlocked());
			abilityPane.add(ability);
			ability.setForeground(Color.GRAY);
			if(abilities.get(i).getLevelUnlocked()<=World.getInstance().getCurrentMap().getAvatar().getLevel()) {
				ability.setForeground(Color.GREEN);
			}
		}
		
		tabbedPane.addTab("Inventory", inventory);
		tabbedPane.addTab("Equipment", equipment);
		tabbedPane.addTab("Abilities", abilityPane);

		textArea.setBackground(Color.BLACK);
		textArea.setForeground(new Color(0, 255, 0));
		this.setPreferredSize(new Dimension(AREA_WIDTH, TOTAL_HEIGHT));
		this.add(textArea, BorderLayout.WEST);
		this.add(tabbedPane, BorderLayout.WEST);
		setVisible(true);
	}
	
	/**
	 * Redraws the StatusViewPort. 
	 */
	public void update() {
		avatar = World.getInstance().getCurrentMap().getAvatar();
		text = new StringBuilder();
		text.append("Name: ").append(avatar.getFullName()).append("\n");
		text.append("Occupation: ").append(avatar.getOccupation()).append("\n\n");
		text.append("Level: ").append(avatar.getLevel()).append("         ");
		text.append("Experience: ").append(avatar.getExperience()).append("\n");
		text.append("Life: ").append(avatar.getCurrLife()).append("/").append(avatar.getMaxLife()).append("      ");
		text.append("Mana: ").append(avatar.getCurrMana()).append("/").append(avatar.getMaxMana()).append("\n");
		text.append("Lives Left: ").append(avatar.getLivesLeft()).append("\n\n");
		text.append("Strength: ").append(avatar.getStrength()).append("     ");
		text.append("Hardiness: ").append(avatar.getHardiness()).append("\n");
		text.append("Agility: ").append(avatar.getAgility()).append("      ");
		text.append("Movement: ").append(avatar.getMovement()).append("\n");
		text.append("Intellect: ").append(avatar.getIntellect()).append("    ");
		text.append("Armor: ").append(avatar.getArmorRating()).append("\n");
		text.append("Offense: ").append(avatar.getOffensiveRating()).append("      ");
		text.append("Defense: ").append(avatar.getDefensiveRating()).append("\n");
		textArea.setText(text.toString());
		ImageLoader img;
		List<TakeableItem> avatarInventory = avatar.getInventory().getItemList();
		for(int i = 0; i < 15; ++i) {
			JLabel item = (JLabel) inventory.getComponent(i);
			if(i>=avatarInventory.size()) {
				item.setIcon(null);
				item.setText("");
				item.setToolTipText(null);
			}
			else {
				item.setForeground(Color.GREEN);
				item.setHorizontalAlignment(SwingConstants.CENTER);
				img = new ImageLoader(avatarInventory.get(i).toString()+"_small.png");
				item.setIcon(img.getImgIcon());
				item.setText("");
				item.setToolTipText(avatarInventory.get(i).getToolTip());
				//item.setText(avatarInventory.get(i).toString());
			}
		}
		EquippedInventory equippedInventory = avatar.getEquippedInventory();
		EquippableItem headItem = equippedInventory.getHeadItem();
		EquippableItem leftItem = equippedInventory.getLeftHandItem();
		EquippableItem rightItem = equippedInventory.getRightHandItem();
		EquippableItem upperItem = equippedInventory.getUpperBodyItem();
		EquippableItem lowerItem = equippedInventory.getLowerBodyItem();
		if(headItem==null) {
			head.setText("head");
			head.setIcon(null);
			head.setToolTipText(null);
		}
		else {
			img = new ImageLoader(headItem.toString()+".png");
			head.setIcon(img.getImgIcon());
			head.setToolTipText(headItem.getToolTip());
			//head.setText("");
			//head.setText(headItem.toString());
		}
		if(leftItem==null) {
			left.setText("left");
			left.setIcon(null);
			left.setToolTipText(null);
		}
		else {
			img = new ImageLoader(leftItem.toString()+".png");
			left.setIcon(img.getImgIcon());
			left.setToolTipText(leftItem.getToolTip());
			//left.setText("");
			//left.setText(leftItem.toString());
		}
		if(rightItem==null) {
			right.setText("right");
			right.setIcon(null);
			right.setToolTipText(null);
		}
		else {
			img = new ImageLoader(rightItem.toString()+".png");
			right.setIcon(img.getImgIcon());
			right.setText("");
			right.setToolTipText(rightItem.getToolTip());
			//right.setText(rightItem.toString());
		}
		if(upperItem==null) {
			upper.setText("upper");
			upper.setIcon(null);
			upper.setToolTipText(null);
		}
		else {
			img = new ImageLoader(upperItem.toString()+".png");
			upper.setIcon(img.getImgIcon());
			upper.setText("");
			upper.setToolTipText(upperItem.getToolTip());
			//upper.setText(upperItem.toString());
		}
		if(lowerItem==null) {
			lower.setText("lower");
			lower.setIcon(null);
			lower.setToolTipText(null);
		}
		else {
			img = new ImageLoader(lowerItem.toString()+".png");
			lower.setIcon(img.getImgIcon());
			lower.setText("");
			lower.setToolTipText(lowerItem.getToolTip());
			//lower.setText(lowerItem.toString());
		}
		Occupation occupation = World.getInstance().getCurrentMap().getAvatar().getOccupation();
		List<Ability> abilities = occupation.getAbilityList();
		for(int i = 0; i < abilities.size(); ++i) {
			JLabel ability = (JLabel) abilityPane.getComponent(i);
			ability.setToolTipText("Level unlocked: "+abilities.get(i).getLevelUnlocked());
			ability.setForeground(Color.GRAY);
			if(abilities.get(i).getLevelUnlocked()<=World.getInstance().getCurrentMap().getAvatar().getLevel()) {
				ability.setForeground(Color.GREEN);
				ability.setHorizontalAlignment(SwingConstants.CENTER);
			}
		}
	}
	
	/**
	 * Returns the default Font.
	 * @return The default Font.
	 */
	private Font initAndGetFont() {
		return new Font("Courier", Font.PLAIN, 13);
	}
	
	
	/**
	 * Returns the JPanel representing the inventory.
	 * @return The JPanel representing the inventory.
	 */
	public JPanel getInventory() {
		return inventory;
	}
	
	/**
	 * Returns the array of JLabels representing the equipped inventory
	 * @return The array of JLabels representing the equipped inventory
	 */
	public JLabel[] getEquipInventory() {
		JLabel[] jl = { head, left, right, upper, lower };
		return jl;
	}
	
	/**
	 * Returns the JPanel representing the ability list.
	 * @return The JPanel representing the ability list.
	 */
	public JPanel getAbilities() {
		return abilityPane;
	}
}